#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/18 0018 16:18
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : for_qiantu
import scrapy
import time
import re
from sct2.items import QiantuItem


class QiantuSpider(scrapy.Spider):
    name = 'qiantu'
    allowed_domains = ["www.58pic.com"]
    start_urls = [
        "http://www.58pic.com/piccate/5-6-1129.html"
    ]

    def parse(self, response):
        print("++++++++++++++++++++++++++++++++++++++++++++")
        for box in response.xpath('//div[@class="card-img"]'):
            item = QiantuItem()
            item['img_alt'] = box.xpath('a/img/@alt')[0].extract()
            try:
                item['img_url'] = box.xpath('a/img/@data-original')[0].extract()[:-6]
            except:
                item['img_url'] = box.xpath('a/img/@src')[0].extract()[:-6]
            item['img_name'] = item['img_alt'] = box.xpath('a/text()')[0].extract()
            next_url = box.xpath('a/@href')[0].extract()
            if next_url:
                yield scrapy.Request(next_url, callback=self.img_size, meta=item)
                time.sleep(1)

    def img_size(self, response):
        item = response.meta
        detail = response.xpath('//div[@class="img-des"]/div[@class="clearfix"]')
        try:
            item['img_pixel'] = detail.xpath('p/text()')[1].extract().strip()
            i = re.search(r'[\D]*(\d*)[\D]*(\d*).*', item['img_pixel'])
            i1 = i.group(1)
            i2 = i.group(2)
            item['img_pixel'] = str(i1 + "x" + i2)
        except:
            item['img_pixel'] = "未获取到"
        try:
            item['img_size'] = detail.xpath('p/text()')[3].extract().strip()[3:]
        except:
            item['img_size'] = "未获取到"
        print(item)
        yield item
